#include "rotateButton.h"
#include "input.h"


RotateButton::RotateButton(){

bActive=false;
name="ROTATION";
}

RotateButton::~RotateButton(){}


//TODO: implement Grid in RotateButton
void RotateButton::update(double deltaTime){

    if (bActive && parent)
      {
          if (parent==input->controller){

              Vector3f rot=parent->rotation;
              input->controller->confineMouse();
              if (input->bShiftDown)
                rot.z+=input->mouseVector.x * renderer->mouseSensitivity;
              else {
                    rot.x+=input->mouseVector.y * renderer->mouseSensitivity;
                    rot.y-=input->mouseVector.x * renderer->mouseSensitivity;
              }

              //TODO: Matrix rotation from button is kindof hacky - should actually read rotation and then apply change!
              if (parent->bUseTransformMatrix)
                parent->addRotation(rot);
              else
                parent->setRotation(rot);
          }
          else{
            for (int i=0;i<(int)input->selectedActors.size();i++){

                  Vector3f rot=input->selectedActors[i]->rotation;
                  input->controller->confineMouse();
                  if (input->bCtrlDown)
                    rot.z+=input->mouseVector.x * renderer->mouseSensitivity;
                  else if (input->bShiftDown)
                    rot.x+=input->mouseVector.y * renderer->mouseSensitivity;
                  else
                    rot.y-=input->mouseVector.x * renderer->mouseSensitivity;
                  //TODO: Matrix rotation from button is kindof hacky - should actually read rotation and then apply change!
                  if (input->selectedActors[i]->bUseTransformMatrix)
                    input->selectedActors[i]->addRotation(rot);
                  else
                    input->selectedActors[i]->setRotation(rot);
                }
          }
      }
}

//start rotation around different axis
void RotateButton::clickedLeft(){

    bActive=true;
    input->focusButton=this;

    if (parent==input->controller){
        fineRotation.push_back(parent->rotation);
        fineLocation.push_back(parent->location);
        }
    else{
        for (int i=0;i<(int)input->selectedActors.size();i++){
            fineRotation.push_back(input->selectedActors[i]->rotation);
            fineLocation.push_back(input->selectedActors[i]->location);
            cout << "active, starting at " << fineLocation[i] << endl;
            }
        }
}

//this is used for connecting to an inputConnectButton
void RotateButton::clickedRight(){

bActive=false;
input->focusButton=this;
cout << "connecting...";
cout << input->hudTarget << endl;
}

//here we connect to the specific button we clicked on (after right clicking the rotate icon)
void RotateButton::focusClick(){

    Button::focusClick();
    //we're now connecting to a inputConnectButton!!!
    if (!bActive){
     if (input->hudTarget && input->hudTarget->isA("inputConnectButton")){
        cout << "connecting with: " << input->hudTarget << endl;
        input->hudTarget->buttonProperty=name;                //which property do we want to connect?
        input->hudTarget->trigger(parent);                          //which actor do we want to connect
        }
     }
    //we've just moved the Actor
    if (bActive)
      bActive=false;
    cout << "inActive!";

    fineRotation.clear();
    fineLocation.clear();
}

void RotateButton::create(){

renderer->addButton(this);
std::cout << "creating a RotateButton!";
}
